package com.huawei.hwclouds.lubanops.demo.config;

import org.jasypt.encryption.StringEncryptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huawei.hwclouds.lubanops.demo.common.util.SCCEncryptUtil;

@Configuration
public class DefaultEncryptor implements StringEncryptor {
    private final static Logger logger = LoggerFactory.getLogger(DefaultEncryptor.class);

    @Bean(name = "jasyptStringEncryptor")
    public static StringEncryptor stringEncryptor() {
        return new DefaultEncryptor();
    }

    @Override
    public String encrypt(String message) {
        try {
            return SCCEncryptUtil.encrypt(message);
        } catch (Exception e) {
            logger.error("encrypt failed:" + e);
            return null;
        }

    }

    @Override
    public String decrypt(String encryptedMessage) {
        try {
            return SCCEncryptUtil.decrypt(encryptedMessage);
        } catch (Exception e) {
            logger.error("decrypt failed:" + e);
            return null;
        }
    }
}
